﻿// 添加到Jquery对象上面，用$("#id").进行访问
; (function ($) {
    "use strict";
    $.fn.formSerialize = function (formdate) {
        var element = $(this);
        if (!!formdate) {
            for (var key in formdate) {
                var $id = element.find('#' + key);
                var value = $.trim(formdate[key]).replace(/&nbsp;/g, '');
                var type = $id.attr('type');
                if ($id.hasClass("select2-hidden-accessible")) {
                    type = "select";
                }
                switch (type) {
                    case "checkbox":
                        if (value == "true") {
                            $id.attr("checked", 'checked');
                        } else {
                            $id.removeAttr("checked");
                        }
                        break;
                    case "select":
                        $id.val(value).trigger("change");
                        break;
                    default:
                        $id.val(value);
                        break;
                }
            };
            return false;
        }
        var postdata = {};
        element.find('input,select,textarea').each(function (r) {
            var $this = $(this);
            var id = $this.attr('Name');
            var type = $this.attr('type');
            switch (type) {
                case "checkbox":
                    postdata[id] = $this.is(":checked");
                    break;
                default:
                    var value = ($this.val() == "" || $this.val() == null) ? "&nbsp;" : $this.val();
                    if (!$.request("keyValue")) {
                        value = value.replace(/&nbsp;/g, '');
                    }
                    postdata[id] = value;
                    break;
            }
        });
        if ($('[name=__RequestVerificationToken]').length > 0) {
            postdata["__RequestVerificationToken"] = $('[name=__RequestVerificationToken]').val();
        }
        return postdata;
    };
    $.request = function (name) {
        var search = location.search.slice(1);
        var arr = search.split("&");
        for (var i = 0; i < arr.length; i++) {
            var ar = arr[i].split("=");
            if (ar[0] == name) {
                if (unescape(ar[1]) == 'undefined') {
                    return "";
                } else {
                    return unescape(ar[1]);
                }
            }
        }
        return "";
    }
})(window.jQuery);